package cn.bgs.serviceuser.MyConfig;

import cn.bgs.serviceedu.entity.Orders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Author:YiChen
 * @program: IntelliJ IDEA
 * @Description:
 * @Created 2021/11/4-15:52
 */
@Component
@Slf4j
public class CallBackConfig implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }



    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        if (b){
            log.info("成功投递消息id为:{}的消息",correlationData.getId());
        }else {
            log.info("消息id:{},错误信息：{}",correlationData.getId(),s);
        }
    }

    @Override
    public void returnedMessage(Message message, int Code, String relyText, String exchange, String routingKey) {
        log.info("消息:{},被{}交换机退回，错误信息：{}，路由key：{},应答码：{}",
                message.getBody(),exchange,relyText,routingKey,Code);

    }
}
